package com.san.hkhospital.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.san.hkhospital.entity.Account;
import com.san.hkhospital.entity.Payin;
import com.san.hkhospital.service.AccountService;
import com.san.hkhospital.service.impl.AccountServiceImpl;
import com.san.hkhospital.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
* (account)表控制层
*
* @author xxxxx
*/
@RestController
@RequestMapping("/account")
public class AccountController {
/**
* 服务对象
*/
@Resource
private AccountServiceImpl accountServiceImpl;
@Autowired
private AccountService accountService;
/**
* 通过主键查询单条数据
*
* @paramid 主键
* @return 单条数据
*/
//@GetMapping("selectOne")
//public Account selectOne(Integer id) {
//return accountServiceImpl.selectByPrimaryKey(id);
//}
//
    @RequestMapping("accountList")
    public ResultVO accountList(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Account> accountList =accountService.accountList();
        PageInfo<Account> pageInfo = new PageInfo<Account>(accountList);
        return ResultVO.success("所有账单查看成功",pageInfo);
    }

    @PostMapping("addAccount")
    public ResultVO addPayIn(@RequestBody Account account) {

        boolean flag = accountService.addAccount(account);
        if (flag) {
            return ResultVO.success("账户记录添加成功");
        }
        return ResultVO.failed("账户记录添加失败");
    }
    @GetMapping("/searchAccount")
    public ResultVO searchAccount(
            @RequestParam Map<String, String> params,
            @RequestParam(value = "startDate", required = false) String startDateStr,
            @RequestParam(value = "endDate", required = false) String endDateStr,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize
    ) {
        try {
            // 手动转换日期参数
            if (startDateStr != null && endDateStr != null) {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                LocalDateTime startDate = LocalDateTime.parse(startDateStr, formatter);
                LocalDateTime endDate = LocalDateTime.parse(endDateStr, formatter);
                params.put("startDate", startDate.toString());
                params.put("endDate", endDate.toString());
            }

            PageHelper.startPage(pageNum, pageSize);
            List<Account> searchList = accountService.searchList(params);
            PageInfo<Account> pageInfo = new PageInfo<>(searchList);
            return ResultVO.success("搜索查询成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVO.failed("搜索查询失败");
        }
    }

    }
